﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net.NetworkInformation;
using System.Net.Mail;
using System.IO;
using System.Text;
using System.Threading;

/// <summary>
/// Various network related opertions;
/// - test computer is reachable via PING
/// - create a folder if it does not exist (home drive)
/// - send an email as notification
/// </summary>
public class NWvarOps
{

    private string srvName = "";
    private long rspTimeInMs = 0;
    private string notifymsg = "";
    private string notifyrcp = "";

    public string SrvName
    {
        get
        {
            return srvName;
        }
        set
        {
            srvName = value;
        }
    }

    public long RspTimeInMs
    {
        get
        {
            return rspTimeInMs;
        }
        set
        {
            rspTimeInMs = value;
        }
    }

	public NWvarOps()
	{
		//
		// TODO: Add constructor logic here
		//
	}

    public string Notifymsg
    {
        get
        {
            return notifymsg;
        }
        set
        {
            notifymsg = value;
        }
    }

    public string Notifyrcp
    {
        get
        {
            return notifyrcp;
        }
        set
        {
            notifyrcp = value;
        }
    }


    public bool ServerReachable(out string retmsg)
    {
        bool retval = false;
        retmsg = "";
        int pingTimeout = 1000;

        try
        {
            Ping cping = new Ping();
            PingReply cpong = cping.Send(srvName, pingTimeout);
            if (cpong.Status == IPStatus.Success)
            {                
                retmsg = srvName + " reached in time: " + cpong.RoundtripTime;
                rspTimeInMs = cpong.RoundtripTime;
                retval = true;
            }                
            else
            {
                retval = false;
            }
        }
        catch (Exception ex)
        {
            retmsg = "Error occured while checking Server. " + srvName + ' ' + ex.StackTrace;
            retval = false;
        }

        return retval;
    }

    public bool CreateFolder(out string retmsg, ADobjInfo adobjin)
    {
        
        retmsg = "";        
        string dirName = adobjin.ADHomeDirve;        
        int HalfSecond = 500;
        
        try
        {
            if (Directory.Exists(dirName))
            {
                retmsg = "The following folder already exists! " + dirName; 
                return false;
            }
            else
            {
                if (rspTimeInMs > HalfSecond)
                {
                    int stmp = HalfSecond + (int)rspTimeInMs;
                    Thread.Sleep(stmp); 
                }                    
                else
                {
                    Thread.Sleep(HalfSecond); 
                }                 
                if (Directory.Exists(dirName))
                {
                    return false;
                }
                else
                {
                    Directory.CreateDirectory(dirName);
                    return true;
                }                                
            }
        }

        catch (Exception ex)
        {
            retmsg = "Error occured while creating folder: " + dirName + ' ' + ex.StackTrace;
            return false;
        }
        
    }

    public void SendMailNotification()
    {

        string mailbody = @"
            <style>
                .rslttablegood {
                    width: 80%;
                    margin: auto;
                    border: solid 1px;
                    font-size: 12px;         
                }
                    .rslttablegood td {        
                        border: solid 1px #26522b;        
                        padding: 5px;   
                    }
                    .rslttablegood tr:nth-of-type(even) {            
                        background-color: #FFFFFF;
                        color: #26522b;
                    }
                    .rslttablegood tr:nth-of-type(odd) {        
                        background-color: #a2c2a5;
                        color: #000000;
                    }
 
                .rslttablesoso {    
                    width: 80%;
                    margin: auto;
                    border: solid 1px;
                    font-size: 12px;        
                }   
                    .rslttablesoso td {        
                        border: solid 1px #ff7e00;            
                        padding: 5px;
                    }
                    .rslttablesoso tr:nth-of-type(even) {        
                        background-color: #ffb266;
                        color: #000000;
                    }
                    .rslttablesoso tr:nth-of-type(odd) {        
                        background-color: #FFFFFF;
                        color: #ff7e00;
                    }

                .rslttablebad {
                    width: 80%;
                    margin: auto;
                    border: solid 1px;
                    font-size: 12px;           
                }
                    .rslttablebad td {        
                        border: solid 1px #ff000d;        
                        padding: 5px;     
                    }
                    .rslttablebad tr:nth-of-type(even) {        
                        background-color: #ff666e;
                        color: #000000;
                    }
                    .rslttablebad tr:nth-of-type(odd) {                 
                        background-color: #FFFFFF;
                        color: #ff000d;
                    }

            </style>
        " + notifymsg + "<br /> In case the application does not react, please close your browser and launch again ...";
        
        string mailSubject = "SACT Notification on: " + DateTime.Now.ToString();
        MailMessage myMessage = new MailMessage(WbGetParams.NotifyFromAddress, Notifyrcp, mailSubject, mailbody);
        myMessage.IsBodyHtml = true;
        MailAddress bcc = new MailAddress(WbGetParams.NotifyToAddress);
        myMessage.Bcc.Add(bcc);            
        
        SmtpClient mySmtpClient = new SmtpClient(WbGetParams.NotifyExchangeServer);

        try
        {
            mySmtpClient.Send(myMessage);
        }
        catch
        {            
        }

    }

}